﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections; 

using YT.DB;
using YT.Commons;
using YT.Modules;
 
namespace YT.XH.NOSE
{
    public partial class frmNSNormalReturnVisitDetail : Form
    {
        private string mstrSeqNo = string.Empty;
        private string mSickRecID = string.Empty;
        private DBAccess mdbcon = new DBAccess(false, false);
        private FormStatus mfrmStatus = new FormStatus();
        private bool mblnExit = false;
        private string mstrTableName="NoseNormalVisitRecordDtl";
        public frmNSNormalReturnVisitDetail(string strSeqNo,string strSickRecID)
        {
            InitializeComponent();
            mstrSeqNo = strSeqNo;
            mSickRecID = strSickRecID;
            if (strSeqNo == "" || strSeqNo == "0")
                mfrmStatus = FormStatus.AddNew;
            else
                mfrmStatus = FormStatus.Update;
        }

        private void initControl()
        {
            txtSickName.Text = DataFunctions.GetFieldValue ("mSickPerson","SickName","SickRecID='"+ mSickRecID +"'");
            if (mfrmStatus == FormStatus.AddNew)
                datVisitDate.Value = BaseFunctions.FormatDate(DateTime.Now.Date.ToString (), DateFormatOption.DateFmtYMD);
        }

        
        
        private void FillContent()
        {
            if (mstrSeqNo == "" || mstrSeqNo == "0") return;
            string strSQL = string.Empty;
            strSQL = "select * from "+ mstrTableName +" where SeqNo='"+ mstrSeqNo +"' and SickRecID='"+ mSickRecID +"' ";
            DataRow drDetail = mdbcon.GetDataFirstRow(strSQL);
            if ( drDetail !=null )
            {
                txtSickName.Text = CvtFunc.ToString ( drDetail["SickName"])   ;
                txtLastWeek_Evaluate.Text = CvtFunc.ToString ( drDetail ["LastWeek_Evaluate"])  ;
                trkLastWeek_Evaluate.Value = CvtFunc.ToInt(txtLastWeek_Evaluate.Text);
                datVisitDate.Text = BaseFunctions.FormatDate(drDetail["VisitDate"].ToString(), DateFormatOption.DateFmtYMD); 

                string strTempValue =string.Empty ;
                #region 鼻塞
                strTempValue =CvtFunc.ToString( drDetail["LastWeek_BS"]);
                if ( strTempValue=="无")  rad1_1.Checked = true ; 
                if ( strTempValue=="轻度") rad1_2.Checked =true ; 
                if ( strTempValue=="中度") rad1_3.Checked =true ; 
                if ( strTempValue=="重度") rad1_4.Checked =true ; 
                #endregion                        
                #region 鼻痒
                strTempValue =CvtFunc.ToString( drDetail["LastWeek_BY"]);
                if ( strTempValue=="无")  rad2_1.Checked = true ; 
                if ( strTempValue=="轻度") rad2_2.Checked =true ; 
                if ( strTempValue=="中度") rad2_3.Checked =true ; 
                if ( strTempValue=="重度") rad2_4.Checked =true ; 
                #endregion        
                #region 流涕
                strTempValue =CvtFunc.ToString( drDetail["LastWeek_PT"]);
                if ( strTempValue=="无")  rad3_1.Checked = true ; 
                if ( strTempValue=="轻度") rad3_2.Checked =true ; 
                if ( strTempValue=="中度") rad3_3.Checked =true ; 
                if ( strTempValue=="重度") rad3_4.Checked =true ; 
                #endregion        
                #region 喷嚏
                strTempValue = CvtFunc.ToString(drDetail["LastWeek_PD"]);
                if ( strTempValue=="无")  rad4_1.Checked = true ; 
                if ( strTempValue=="轻度") rad4_2.Checked =true ; 
                if ( strTempValue=="中度") rad4_3.Checked =true ; 
                if ( strTempValue=="重度") rad4_4.Checked =true ; 
                #endregion                                                

             if ( CvtFunc.ToBoolean (drDetail["LastWeekUseDrug"]))
                 radLastWeekUseDrug_Yes.Checked =true ;
             else 
                 radLastWeekUseDrug_No.Checked =true ;


             txtOraldrugName.Text = CvtFunc.ToString(drDetail["OraldrugName"]);
             txtOraldrugTimes.Text = CvtFunc.ToString(drDetail["OraldrugTimes"]);
             txtSparydrugName.Text = CvtFunc.ToString(drDetail["SparydrugName"]);
             txtSparydrugTimes.Text = CvtFunc.ToString(drDetail["SparyTimes"]);
             txtOthers.Text = CvtFunc.ToString(drDetail["Others"]);
            }
        }

        private bool ValidCheck()
        {
            string strErrMSG = string.Empty;
            if (!BaseFunctions.IsDate(datVisitDate.Value))
            {
                strErrMSG = "请输入回访日期。";
            }            
            else if (txtLastWeek_Evaluate.Text.ToString().Trim() == "")
            {
                strErrMSG = "请输入 视觉模拟评分 ";
            }
            else if (!rad1_1.Checked && !rad1_2.Checked && !rad1_3.Checked && !rad1_4.Checked)
            {
                strErrMSG = "请选择 鼻塞 的主要症状。";
            }
            else if (!rad2_1.Checked && !rad2_2.Checked && !rad2_3.Checked && !rad2_4.Checked)
            {
                strErrMSG = "请选择 鼻痒 的主要症状。";
            }
            else if (!rad3_1.Checked && !rad3_2.Checked && !rad3_3.Checked && !rad3_4.Checked)
            {
                strErrMSG = "请选择 流涕 的主要症状。";
            }
            else if (!rad4_1.Checked && !rad4_2.Checked && !rad4_3.Checked && !rad4_4.Checked)
            {
                strErrMSG = "请选择 喷嚏 的主要症状。";
            }
            else if (!radLastWeekUseDrug_Yes.Checked && !radLastWeekUseDrug_No.Checked)
            {                
                strErrMSG = "你选择最近一周用药情况。";
            }
            else if (radLastWeekUseDrug_Yes.Checked == true)
            {
                if ((txtOraldrugName.Text.ToString().Trim() == "") && (txtSparydrugName.Text.ToString().Trim() == "") && txtOthers.Text.ToString().Trim() == "")
                {
                    strErrMSG = "你必须输入药品名称。";
                }
            }
            if (strErrMSG != "")
            {
                MessageBox.Show(strErrMSG, "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;

        }

        private bool UpdateDetail()
        {
            string strSQL = string.Empty;
            DataRow drRow = null;
            string strSeqNo = string.Empty;
            if (!ValidCheck()) return false;
            if (mfrmStatus == FormStatus.Update)
            {
                strSQL = "select top 1 * from "+ mstrTableName +" where SeqNo='"+ mstrSeqNo +"' and SickRecID='"+ mSickRecID +"' ";
                drRow = mdbcon.GetDataFirstRow(strSQL);
                strSeqNo = CvtFunc.ToString(drRow["SeqNo"]);
            }
            else if (mfrmStatus == FormStatus.AddNew)
            {
                drRow = mdbcon.GetDataTable("select top 0 * from " + mstrTableName + " ").NewRow();
                strSeqNo = (CvtFunc.ToInt(DataFunctions.GetFieldValue(mstrTableName, "max(SeqNo) as SeqNo", " 1=1 and SickRecID='"+ mSickRecID +"' ")) + 1).ToString();
            }
            
            drRow["SickName"] = txtSickName.Text;
            drRow["SeqNo"] = strSeqNo;
            drRow["SickRecID"] = mSickRecID;
            drRow["VisitDate"] = BaseFunctions.FormatDate(datVisitDate.Value, DateFormatOption.DateFmtYMD);
            drRow["LastWeekUseDrug"]= ( radLastWeekUseDrug_Yes.Checked == true ? 1:0 );
            drRow["LastWeek_Evaluate"] = CvtFunc.ToInt ( txtLastWeek_Evaluate.Text );

            
            string strTempValue = string.Empty;
            #region 鼻塞
            if (rad1_1.Checked) strTempValue = rad1_1.Text;
            if (rad1_2.Checked) strTempValue = rad1_2.Text;
            if (rad1_3.Checked) strTempValue = rad1_3.Text;
            if (rad1_4.Checked) strTempValue = rad1_4.Text;
            drRow["LastWeek_BS"] = strTempValue;
            #endregion

            #region 鼻痒
            if (rad2_1.Checked) strTempValue = rad2_1.Text;
            if (rad2_2.Checked) strTempValue = rad2_2.Text;
            if (rad2_3.Checked) strTempValue = rad2_3.Text;
            if (rad2_4.Checked) strTempValue = rad2_4.Text;
            drRow["LastWeek_BY"] = strTempValue;
            #endregion

            #region 流涕
            if (rad3_1.Checked) strTempValue = rad3_1.Text;
            if (rad3_2.Checked) strTempValue = rad3_2.Text;
            if (rad3_3.Checked) strTempValue = rad3_3.Text;
            if (rad3_4.Checked) strTempValue = rad3_4.Text;
            drRow["LastWeek_PT"] = strTempValue;
            #endregion
            #region 喷嚏
            if (rad4_1.Checked) strTempValue = rad4_1.Text;
            if (rad4_2.Checked) strTempValue = rad4_2.Text;
            if (rad4_3.Checked) strTempValue = rad4_3.Text;
            if (rad4_4.Checked) strTempValue = rad4_4.Text;
            drRow["LastWeek_PD"] = strTempValue;
            #endregion
            if (pnlLastWeekUseDrug.Enabled == true)
            {
                drRow["OraldrugName"] = txtOraldrugName.Text;
                drRow["OraldrugTimes"] = CvtFunc.ToInt(txtOraldrugTimes.Text);
                drRow["SparydrugName"] = txtSparydrugName.Text;
                drRow["SparyTimes"] = CvtFunc.ToInt(txtSparydrugTimes.Text);
                drRow["Others"] = CvtFunc.ToString(txtOthers.Text);
            }
            else
            {
                drRow["OraldrugName"] = "";
                drRow["OraldrugTimes"] = CvtFunc.ToInt("0");
                drRow["SparydrugName"] = "";
                drRow["SparyTimes"] = CvtFunc.ToInt("0");
                drRow["Others"] = "";

            }

            string strFields="SeqNo,SickRecID,SickName,VisitDate,LastWeek_Evaluate,LastWeek_BS,LastWeek_BY,LastWeek_PT,LastWeek_PD,LastWeekUseDrug,OraldrugName,OraldrugTimes,SparydrugName,SparyTimes,Others,Remark";
            if (mfrmStatus == FormStatus.AddNew)
                DataFunctions.UpdateTable(mdbcon, strFields, mstrTableName, drRow, FormStatus.AddNew);
            else
                DataFunctions.UpdateTable(mdbcon, strFields, mstrTableName, drRow, FormStatus.Update);

            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!UpdateDetail())
                return;
            CloseFrom();
        }

        private void frmNSNormalReturnVisitDetail_FormClosed(object sender, FormClosedEventArgs e)
        {           
            mdbcon.CloseCon(); 
        }

        private void timer1_Tick(object sender, EventArgs e)
        {         
            if (mblnExit == false)
            {
                if (this.Opacity >= 1)
                {       
                    timer1.Stop();                    
                }
                this.Opacity += 0.04;
            }
            else
            {                
                this.Close();                 
            }
        }

        private void frmNSNormalReturnVisitDetail_Load(object sender, EventArgs e)
        {
            this.Text = "患者回访情况调查表";
            FillContent();
            initControl();
            timer1.Start();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                if (!UpdateDetail()) return;

            Application.DoEvents();
            this.Refresh();
            Application.DoEvents();            
            CloseFrom();
        }

        private void trkLastWeek_Evaluate_Scroll(object sender, EventArgs e)
        {
            txtLastWeek_Evaluate.Text = trkLastWeek_Evaluate.Value.ToString();
        }

        private void CloseFrom()
        {
            mblnExit = true;
            this.Close();    
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Application.DoEvents();
            base.OnPaint(e);
            
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            Application.DoEvents();
            base.OnPaintBackground(e);
            
        }

        private void radLastWeekUseDrug_No_CheckedChanged(object sender, EventArgs e)
        {
            if (radLastWeekUseDrug_No.Checked == true)
                pnlLastWeekUseDrug.Enabled = false;
            else
                pnlLastWeekUseDrug.Enabled = true;
        }

        private void radLastWeekUseDrug_Yes_CheckedChanged(object sender, EventArgs e)
        {
            if (radLastWeekUseDrug_Yes.Checked == true)
                pnlLastWeekUseDrug.Enabled = true ;
            else
                pnlLastWeekUseDrug.Enabled = false;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            FormPrinting.FormPrinting frmPrint = new FormPrinting.FormPrinting(this);
            frmPrint.Print(); 
        }

        private void frmNSNormalReturnVisitDetail_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!mblnExit)
            {
                if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (!UpdateDetail()) e.Cancel = true;
                }
            }
        }

       

    }
}
